﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
    public class DB_ChooseAction
    {
        public bool InsertAction(ChooseAction action)
        {
            if (!CheckActionExist(action.ItemName))
            {
                return false;
            }
            string sql = "insert into chooseaction values(@ItemName)";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ItemName",action.ItemName)
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool UpdateAction(ChooseAction action)
        {
            string sql = "update chooseaction set ItemName=@ItemName where itemid=@itemId";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@ItemName",action.ItemName),
                new SqlParameter("@itemId",action.ItemId),
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool DeleteAction(int itemId)
        {
            string sql = "delete chooseaction where itemid=@itemid";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@itemId",itemId),
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public List<ChooseAction> GetAllAction()
        {
            List<ChooseAction> list = new List<ChooseAction>();
            String sql = "select * from chooseaction";

            DataTable dt = new DataTable();

            try
            {
                dt = SQLHELP.GetDataTableNoParm(sql);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ConvertListAndTable.DataTableToList<ChooseAction>(dt);
            }
        }

        private bool CheckActionExist(string itemName)
        {
            string sql = "select count(*) from ChooseAction where itemName=@itemName";
            SqlParameter[] parm = new SqlParameter[] {
                new SqlParameter("@itemName",itemName)
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }

            if (count == -1)
            {
                return true;
            }
            return false;
        }
    }
}
